package it.must.myspringboot.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

/**
 * @Author zj@uinpay.cn
 * @Date 2024/12/6 15:43
 */

@Aspect
@Component // 或者使用 @Configuration 和 @EnableAspectJAutoProxy
public class LoggingAspect {

    // 环绕通知
    @Around("execution(* it.must.myspringboot.service.*.*(..))")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();

        // 在目标方法执行之前执行的逻辑
        System.out.println("Method " + joinPoint.getSignature().getName() + " starts");

        // 执行目标方法
        Object proceed = joinPoint.proceed();

        // 在目标方法执行之后执行的逻辑
        long endTime = System.currentTimeMillis();
        System.out.println("Method " + joinPoint.getSignature().getName() + " ends. Execution time: " + (endTime - startTime) + " ms");

        return proceed;
    }
}
